package huayang.ws.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * Created by buhuayang on 16/7/8.
 */
public class JacksonUtil {

    private static final Logger logger = LoggerFactory.getLogger(JacksonUtil.class);

    private static final ObjectMapper objectMapper = new ObjectMapper();

    public static String writeObj2Json(Object obj) {
        try {
            return objectMapper.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            logger.error(e.getMessage(), e);
            return "";
        }
    }

    public static <T> T readJson2Obj(String jsonStr, Class<T> classType) {
        try {
            return objectMapper.readValue(jsonStr, classType);
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
            return null;
        }
    }


}
